package baseclass.b_stackQueue;

import java.util.Arrays;

/**
 * 数组模拟栈
 * @date 2020/2/27 16:56
 */
public class Code01_ArrayStack {
    private int capacity;
    private int size;
    private int []stack;

    public Code01_ArrayStack(int capacity) {
        this.capacity = capacity;
        this.stack = new int[capacity];
        this.size = 0;
    }

    public void push(int e){
        if(size >= capacity){
            //扩容1.5倍
//            throw new RuntimeException("full");
            this.capacity += (capacity >> 1) ;
           stack = Arrays.copyOf(stack,capacity);
        }
        stack[size++] = e;
    }
    public int pop(){
        if(size <= 0){
            throw new RuntimeException("empty");
        }
        return stack[--size];
    }
    public int getSize(){
        return this.size;
    }
    public boolean isEmpty(){
        return this.size == 0;
    }

    public static void main(String[] args) throws InterruptedException {
        Code01_ArrayStack stack = new Code01_ArrayStack(10);
        for (int i = 1; i <= 10 ; i++) {
            stack.push(i);
        }
        System.out.println(stack.getSize());
        stack.push(11);
        System.out.println(stack.capacity);
        /*while (!stack.isEmpty())
            System.out.println(stack.pop());
        Thread.sleep(1000l);
        stack.pop();
        System.out.println(stack.getSize());*/
    }
}
